{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import glob\n",
    "import os\n",
    "import subprocess\n",
    "import sys\n",
    "\n",
    "sys.path.append(\"./src\")\n",
    "try:\n",
    "    sys.path.append(\n",
    "        glob.glob(\n",
    "            \"%s/PythonAPI/carla/dist/carla-*%d.%d-%s.egg\"\n",
    "            % (\n",
    "                os.environ.get(\"CARLA_ROOT\", \".\"),\n",
    "                sys.version_info.major,\n",
    "                sys.version_info.minor,\n",
    "                \"win-amd64\" if os.name == \"nt\" else \"linux-x86_64\",\n",
    "            )\n",
    "        )[0]\n",
    "    )\n",
    "except IndexError:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import carla\n",
    "from carla_world import CarlaWorld"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Initilize Connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define settings\n",
    "sync = False\n",
    "fixed_delta_seconds = None\n",
    "reload = True\n",
    "host = \"127.0.0.1\"\n",
    "port = 2000\n",
    "map_name = \"Town01\"\n",
    "npc_vehicle_num = 60\n",
    "npc_walker_num = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = carla.Client(host, port)\n",
    "client.set_timeout(10.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test if the server is ready\n",
    "print(client.get_server_version())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get static assets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "carla_world = CarlaWorld(client)\n",
    "if map_name not in carla_world.map.name:\n",
    "    carla_world.load_world(map_name)\n",
    "elif reload:\n",
    "    carla_world.reload_world()\n",
    "\n",
    "carla_world.toggle_sync_mode(sync, fixed_delta_seconds)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get Ego Actor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# If you already have a server running with vehicles\n",
    "# you can use the following code to get the vehicle you want\n",
    "hero_actor = carla_world.find_actor_by_name(\"hero\")\n",
    "if hero_actor is None:\n",
    "    print(\"No hero vehicle found, spawning a new one\")\n",
    "    hero_actor = carla_world.spawn_actor(\"hero\", \"vehicle.tesla.model3\")\n",
    "\n",
    "carla_world.tick()\n",
    "carla_world.set_view(hero_actor.get_transform(), \"behind\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "carla_world.follow_actor_view(hero_actor, \"above\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# carla_world.stop_follow_actor_view()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Manual Control"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "control_proc = subprocess.Popen(\n",
    "    [\n",
    "        \"python\",\n",
    "        \"examples/manual_control.py\",\n",
    "        \"--host\",\n",
    "        host,\n",
    "        \"--port\",\n",
    "        str(port),\n",
    "        \"--rolename\",\n",
    "        hero_actor.attributes[\"role_name\"],\n",
    "        \"--res\",\n",
    "        \"800x600\",\n",
    "        \"--keep_ego_vehicle\",\n",
    "    ],\n",
    "    stdout=subprocess.DEVNULL,\n",
    "    stderr=subprocess.STDOUT,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate traffic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "traffic_proc = subprocess.Popen(\n",
    "    [\n",
    "        \"python\",\n",
    "        \"examples/generate_traffic.py\",\n",
    "        \"--host\",\n",
    "        host,\n",
    "        \"--port\",\n",
    "        str(port),\n",
    "        \"-n\",\n",
    "        str(npc_vehicle_num),\n",
    "        \"-w\",\n",
    "        str(npc_walker_num),\n",
    "        \"--safe\",\n",
    "        \"--asynch\",\n",
    "    ],\n",
    "    stdout=subprocess.DEVNULL,\n",
    "    stderr=subprocess.STDOUT,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Render Bounding Box to Camera"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bbox_proc = subprocess.Popen(\n",
    "    [\n",
    "        \"python\",\n",
    "        \"examples/sensor_bounding_box.py\",\n",
    "        \"--host\",\n",
    "        host,\n",
    "        \"--port\",\n",
    "        str(port),\n",
    "        \"--rolename\",\n",
    "        hero_actor.attributes[\"role_name\"],\n",
    "        \"--view\",\n",
    "        \"above\",\n",
    "    ],\n",
    "    stdout=subprocess.DEVNULL,\n",
    "    stderr=subprocess.STDOUT,\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test Space, Do what you like"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from local_carla_api import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "87f28dd1784d49e7e51a9a075fc92db54f5248d4c9ebb22a3e278ba15cc054c2"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
